package com.ctbri.vSearch.bean.dto;

import java.io.Serializable;
import java.util.Set;

public class SearchItem implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7590728329774468038L;
	
	private String content = "";
	private String url = "";
	private String title = "";
	private String keyword = "";
	private Set tmp;
	private  double score;

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

	public Set getTmp() {
		return tmp;
	}

	public void setTmp(Set tmp) {
		this.tmp = tmp;
	}


	@Override
	public String toString() {
		return "SearchItem{" +
				"content='" + content + '\'' +
				", url='" + url + '\'' +
				", title='" + title + '\'' +
				", keyword='" + keyword + '\'' +
				", tmp=" + tmp +
				", score=" + score +
				'}';
	}

	public SearchItem(String body, String url, String keyword, String title) {
		super();
		this.content = parseContent(body,keyword);
		this.url = url;
		this.title = toBold(title, keyword);
		this.keyword=keyword;
	}

	public SearchItem() {
	}

	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	private String parseContent(String body,String keyword){
		String[] res = body.split("\\n");
		String result = "";
		
		for(String tmp:res){
				result+=tmp.replaceAll("#", "").replaceAll("_","");
			if(result.length()>350) {
				result=result.substring(0, 330);
				break;
			} 
		}
		result = toBold(result,keyword);
		return result;
	}
	
	private String toBold(String content,String keyword){
		if(content.indexOf(keyword)>=0){
			return content.replace(keyword, "<strong>"+keyword+"</strong>");
		}else{
			return content;
		}
	}
	
	@Override
	public boolean equals(Object obj){
		if(obj instanceof SearchItem){
			SearchItem searchItem = (SearchItem)obj;
			return searchItem.title.equals(this.title);
		}
		return super.equals(obj);
		
	}
	 
}

